package com.xin.data.simbapesa.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewbinding.ViewBinding;

import com.xin.data.simbapesa.R;
import com.xin.data.simbapesa.SimbaApp;
import com.xin.data.simbapesa.api.PU;
import com.xin.data.simbapesa.base.BaseBindingAdapter1;
import com.xin.data.simbapesa.databinding.SimbaMsgItem1Binding;
import com.xin.data.simbapesa.databinding.SimbaMsgItem2Binding;
import com.xin.data.simbapesa.databinding.SimbaMsgItem3Binding;
import com.xin.data.simbapesa.databinding.SimbaMsgItem4Binding;
import com.xin.data.simbapesa.obj.SimbaAppInfo;
import com.xin.data.simbapesa.obj.SimbaChatInfo;
import com.xin.data.simbapesa.obj.SimbaCustomerService;
import com.xin.data.simbapesa.utils.ResUtils;
import com.xin.data.simbapesa.view.SimbaSpaceItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class MsgAdapter extends BaseBindingAdapter1<SimbaChatInfo, ViewBinding> {


    int code;

    BtnItem3ClickListener onBtnItem3ClickListener;

    public MsgAdapter(List<SimbaChatInfo> dataList) {
        super(dataList);
    }

    @Override
    protected void bindData(ViewBinding binding, SimbaChatInfo item, int position) {


        if (binding instanceof SimbaMsgItem1Binding) {
            SimbaMsgItem1Binding binding1 = (SimbaMsgItem1Binding) binding;
            binding1.tvTitle.setText(item.getTalkInfo());
            binding1.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onBtnItemClickListener != null) {
                        onBtnItemClickListener.onItemClick(position);
                    }
                }
            });
        } else if (binding instanceof SimbaMsgItem2Binding) {
            SimbaMsgItem2Binding binding1 = (SimbaMsgItem2Binding) binding;
            binding1.tvTitle.setText(item.getTalkInfo());
            if (item.getState() == 0) {
                binding1.tvIcon2.setImageResource(R.mipmap.msg_state1);
            } else {
                binding1.tvIcon2.setImageResource(R.mipmap.msg_state2);
            }
            if (position == dataList.size() - 1) {
                binding1.tvHit.setVisibility(View.VISIBLE);
            } else {
                binding1.tvHit.setVisibility(View.GONE);
            }
            binding1.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onBtnItemClickListener != null) {
                        onBtnItemClickListener.onItemClick(position);
                    }
                }
            });
        } else if (binding instanceof SimbaMsgItem3Binding) {
            SimbaMsgItem3Binding binding1 = (SimbaMsgItem3Binding) binding;
            List<String> itemList = new ArrayList<>();

            if (code == 1) {
                itemList.add(ResUtils.s(R.string.msgtitle_title1));
                itemList.add(ResUtils.s(R.string.msgtitle_title2));
                itemList.add(ResUtils.s(R.string.msgtitle_title3));
            } else if (code == 2) {
                itemList.add(ResUtils.s(R.string.msgtitle_title4));
                itemList.add(ResUtils.s(R.string.msgtitle_title5));
                itemList.add(ResUtils.s(R.string.msgtitle_title6));
            } else if (code == 3) {
                itemList.add(ResUtils.s(R.string.msgtitle_title7));
                itemList.add(ResUtils.s(R.string.msgtitle_title8));
                itemList.add(ResUtils.s(R.string.msgtitle_title9));
            } else if (code == 4) {
                itemList.add(ResUtils.s(R.string.msgtitle_title10));
                itemList.add(ResUtils.s(R.string.msgtitle_title11));
                itemList.add(ResUtils.s(R.string.msgtitle_title12));
            } else if (code == 5) {
                itemList.add(ResUtils.s(R.string.msgtitle_title13));
                itemList.add(ResUtils.s(R.string.msgtitle_title14));
                itemList.add(ResUtils.s(R.string.msgtitle_title15));
            }

            itemList.add(ResUtils.s(R.string.msgtitle_title16));
            itemList.add(ResUtils.s(R.string.msgtitle_title17));


            MsgTitleAdapter msgTitleAdapter = new MsgTitleAdapter(itemList);
            msgTitleAdapter.setOnBtnItemClickListener(position1 -> {
                if (onBtnItem3ClickListener != null) {
                    onBtnItem3ClickListener.onItemClick(itemList.get(position1), position1);
                }
            });

            LinearLayoutManager layoutManager1 = new LinearLayoutManager(binding1.recyclerView.getContext());
            binding1.recyclerView.setLayoutManager(layoutManager1);
            int d = ResUtils.d(R.dimen.dp_6);
            SimbaSpaceItemDecoration decoration = new SimbaSpaceItemDecoration(d, 0, binding1.recyclerView.getContext());
            binding1.recyclerView.addItemDecoration(decoration);
            binding1.recyclerView.setAdapter(msgTitleAdapter);

        } else if (binding instanceof SimbaMsgItem4Binding) {
            SimbaMsgItem4Binding binding1 = (SimbaMsgItem4Binding) binding;
            List<SimbaCustomerService> csList = new ArrayList<>();
            CsAdapter adater = new CsAdapter(csList);
            LinearLayoutManager manager = new LinearLayoutManager(binding1.getRoot().getContext());
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            binding1.recyclerView.setLayoutManager(manager);
            binding1.recyclerView.addItemDecoration(new SimbaSpaceItemDecoration(0, 0, binding1.getRoot().getContext()));
            adater.setOnBtnItemClickListener(p -> {
                SimbaCustomerService cs = csList.get(p);
                if (cs.isPhone()) {
                    PU.userBuriedPoint(PU.P63, "");
                    toCall(cs.getData(), binding1.getRoot().getContext());
                }
            });
            binding1.recyclerView.setAdapter(adater);
            addCsList(csList, adater);
        }

    }

    void addCsList(List<SimbaCustomerService> csList, CsAdapter adater) {
        SimbaAppInfo appInfo = SimbaApp.getInstance().getAppInfo();
        csList.clear();
        String mobile = appInfo.getMobile();
        String email = appInfo.getEmail();
        if (!TextUtils.isEmpty(mobile)) {
            if (mobile.contains(",")) {
                String[] mobileList = mobile.split(",");
                for (String s : mobileList) {
                    SimbaCustomerService bean = new SimbaCustomerService();
                    bean.setPhone(true);
                    bean.setData(s);
                    csList.add(bean);
                }
            } else {
                SimbaCustomerService bean = new SimbaCustomerService();
                bean.setPhone(true);
                bean.setData(mobile);
                csList.add(bean);
            }
        }
        if (!TextUtils.isEmpty(email)) {
            if (email.contains(",")) {
                String[] emailList = email.split(",");
                for (String s : emailList) {
                    SimbaCustomerService bean = new SimbaCustomerService();
                    bean.setPhone(false);
                    bean.setData(s);
                    csList.add(bean);
                }
            } else {
                SimbaCustomerService bean = new SimbaCustomerService();
                bean.setPhone(false);
                bean.setData(email);
                csList.add(bean);
            }
        }
        adater.updateAllData();
    }

    void toCall(String phoneNumber, Context context) {
        Intent dialIntent = new Intent(Intent.ACTION_DIAL);
        dialIntent.setData(Uri.parse("tel:" + phoneNumber));
        context.startActivity(dialIntent);
    }

    @Override
    protected int getItemType(SimbaChatInfo item) {
        if (item.isPhone()) {
            return 4;
        } else if (TextUtils.isEmpty(item.getTalkType())) {
            return 3;
        } else if (item.getTalkType().equals("CS")) {
            return 1;
        }
        return 2;
    }

    @Override
    protected ViewBinding createBinding(LayoutInflater inflater, ViewGroup parent, int viewType) {
        if (viewType == 1) {
            return SimbaMsgItem1Binding.inflate(inflater, parent, false);
        } else if (viewType == 2) {
            return SimbaMsgItem2Binding.inflate(inflater, parent, false);
        } else if (viewType == 4) {
            return SimbaMsgItem4Binding.inflate(inflater, parent, false);
        }
        return SimbaMsgItem3Binding.inflate(inflater, parent, false);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public interface BtnItem3ClickListener {
        void onItemClick(String item, int position);
    }

    public void setOnBtnItem3ClickListener(BtnItem3ClickListener onBtnItem3ClickListener) {
        this.onBtnItem3ClickListener = onBtnItem3ClickListener;
    }
}
